package de.fmannan.addbook.menu.actions;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;

import org.apache.log4j.Logger;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.HelpListener;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

import de.fmannan.addbook.MyImageRegistry;
import de.fmannan.addbook.MyMessageRegistry;
import de.fmannan.addbook.domainmodel.GroupRegistrar;
import de.fmannan.addbook.persistence.xmlwriter.XMLWriter;

public class SaveAddressBookAction implements IWorkbenchAction {

	// Unique identifier
	private static final String ID = "de.fmannan.addbook.menu.actions.SaveAddressBookAction";
	private LinkedList<IPropertyChangeListener> listenerList = new LinkedList<IPropertyChangeListener>();
	private int keyAccelerator = SWT.F2;
	private String tooltip = MyMessageRegistry.SAVE_APPLICATION_TOOLTIP;
	private String text = MyMessageRegistry.SAVE_APPLICATION_TEXT;
	
	private static final Logger log = Logger.getLogger(SaveAddressBookAction.class);
	private static final String pathToSaveFile = Platform.getLocation()
			.toString()
			+ "/save.xml";

	
	@Override
	public void dispose() {
		// TODO no idea how to implement it
	}

	@Override
	public void addPropertyChangeListener(IPropertyChangeListener listener) {
		this.listenerList.add(listener);
	}

	@Override
	public int getAccelerator() {
		// This action (save current address-book data can be activated by using
		// the F2 Button
		return keyAccelerator;
	}

	@Override
	public String getActionDefinitionId() {
		// Not required
		return null;
	}

	@Override
	public String getDescription() {
		return getToolTipText();
	}

	@Override
	public ImageDescriptor getDisabledImageDescriptor() {
		// TODO Find an image that matches the save button but looks as if
		// disabled
		return null;
	}

	@Override
	public HelpListener getHelpListener() {
		// TODO No HelpListener implemented as of yet and beside it doesnt seem
		// necessary for a simple save button
		return null;
	}

	@Override
	public ImageDescriptor getHoverImageDescriptor() {
		return MyImageRegistry.getImageRegistry().getDescriptor(
				MyImageRegistry.SAVE_ICON);
	}

	@Override
	public String getId() {
		// Returns an unique identifier
		return ID;
	}

	@Override
	public ImageDescriptor getImageDescriptor() {
		return MyImageRegistry.getImageRegistry().getDescriptor(
				MyImageRegistry.SAVE_ICON);
	}

	@Override
	public IMenuCreator getMenuCreator() {
		// TODO no idea how to implement it, returning null for now
		return null;
	}

	@Override
	public int getStyle() {
		// The save action is displayed as a push-button
		return SWT.CHECK;
	}

	@Override
	public String getText() {
		return text;
	}

	@Override
	public String getToolTipText() {
		return tooltip;
	}

	@Override
	public boolean isChecked() {
		// Only applies to checkboxes and radio buttons
		return false;
	}

	@Override
	public boolean isEnabled() {
		// Saving should always be possible regardless of the current situation
		return true;
	}

	@Override
	public boolean isHandled() {
		// No delegation to other objects takes place
		return true;
	}

	@Override
	public void removePropertyChangeListener(IPropertyChangeListener listener) {
		this.listenerList.remove(listener);
	}

	@Override
	public void run() {
		log.debug("Saving current addressbook");
		// Start Saving the Domain model to file
		try {			
			XMLWriter out = new XMLWriter(new BufferedWriter(new FileWriter(
					pathToSaveFile)));
			GroupRegistrar.getInstance().writeToXML(out);			
		} catch (IOException e) {
			log.error("IOException occured while writing the domain Model to XML", e);
		}
	}

	@Override
	public void runWithEvent(Event event) {
		this.run();
	}

	@Override
	public void setAccelerator(int keycode) {
		this.keyAccelerator = keycode;
	}

	@Override
	public void setActionDefinitionId(String id) {
		// Not required
	}

	@Override
	public void setChecked(boolean checked) {
		// Not required since its a simple Push-button
	}

	@Override
	public void setDescription(String text) {
		this.tooltip = text;
	}

	@Override
	public void setDisabledImageDescriptor(ImageDescriptor newImage) {
		// Not required for now
	}

	@Override
	public void setEnabled(boolean enabled) {
		// We want it to be enabled all the time
	}

	@Override
	public void setHelpListener(HelpListener listener) {
		// TODO Auto-generated method stub
	}

	@Override
	public void setHoverImageDescriptor(ImageDescriptor newImage) {
		// Same as normal ImageDescriptor
	}

	@Override
	public void setId(String id) {
		// ID cant be changed
	}

	@Override
	public void setImageDescriptor(ImageDescriptor newImage) {
		// can't be changed
	}

	@Override
	public void setMenuCreator(IMenuCreator creator) {
		// Not required
	}

	@Override
	public void setText(String text) {
		text = MyMessageRegistry.SAVE_APPLICATION_TEXT;
	}

	@Override
	public void setToolTipText(String text) {
		tooltip = text;
	}

	public static String getPathToSaveFile() {
		return pathToSaveFile;
	}

}
